import sys, os, shutil, re

def errorinfo(tips):
    print "ERROR:commit message doesn't match standard format,please check!"
    print "======standard format ========="
    print "Module: modification summary"
    print "                            "
    print "commit_type:                "
    print "commit_detail:              "
    print "reason:...                  "
    print "ext-change interface name:..."
    print "ext-change header files:..."
    print "ext-change type:..."
    print "ext-change describe:..."
    print "ext-change influences:..."
    print "ext-change document:..."
    print "ext-change others:..."
    print "============================"
    print tips
    sys.exit(1)

def msg_check(msg):
    summary = msg.split("\n")

    #check revert msg
    list = re.findall('^\s*Revert*',summary[0])
    if 1 == len(list):
        if 5 == len(summary):
            sys.exit(0)
    
    #check commit msg or cherrypick msg
    if "ext-change interface name:" in msg:
        log = msg.split("ext-change interface name:")[1].split("\n")[0].replace(' ','')
        print "log1 = " + log
        list = re.findall('.*\w+',log)
        if 1 > len(list):
            list = re.findall(ur'[\u4e00-\u9fa5]',log.decode('utf-8'))
            if 1 > len(list):
                errorinfo("Error No.13: Some error on ext-change interface name:.*\w+")
    else:
        errorinfo("Error No.14: Some error on ext-change interface name:.*\w+")
    if "No" == log or "no" == log or "NO" == log:
        print "[ext-change interface name] be set as " + log + " , so check over."
        return 0
    if "ext-change header files:" in msg:
        name = msg.split("ext-change header files:")[1].split("\n")[0]
        list = re.findall('.*\w+',name)
        if 1 > len(list):
            list = re.findall(ur'[\u4e00-\u9fa5]',name.decode('utf-8'))
            if 1 > len(list):
                errorinfo("Error No.15: Some error on ext-change header files:.*\w+")
    else:
        errorinfo("Error No.16: Some error on ext-change header files:.*\w+")
    if "ext-change type:" in msg:
        name = msg.split("ext-change type:")[1].split("\n")[0].replace(' ','')
        list = re.findall('.*\w+',name)
        if 1 > len(list):
            list = re.findall(ur'[\u4e00-\u9fa5]',name.decode('utf-8'))
            if 1 > len(list):
                errorinfo("Error No.17: Some error on ext-change type:.*\w+")
    else:
        errorinfo("Error No.18: Some error on ext-change type:.*\w+")
    if "ext-change describe:" in msg:
        name = msg.split("ext-change describe:")[1].split("\n")[0]
        list = re.findall('.*\w+',name)
        if 1 > len(list):
            list = re.findall(ur'[\u4e00-\u9fa5]',name.decode('utf-8'))
            if 1 > len(list):
                errorinfo("Error No.19: Some error on ext-change describe:.*\w+")
    else:
        errorinfo("Error No.20: Some error on ext-change describe:.*\w+")
    if "ext-change influences:" in msg:
        name = msg.split("ext-change influences:")[1].split("\n")[0]
        list = re.findall('.*\w+',name)
        if 1 > len(list):
            list = re.findall(ur'[\u4e00-\u9fa5]',name.decode('utf-8'))
            if 1 > len(list):
                errorinfo("Error No.21: Some error on ext-change influences:.*\w+")
    else:
        errorinfo("Error No.22: Some error on ext-change influences:.*\w+")
    if "ext-change document:" in msg:
        name = msg.split("ext-change document:")[1].split("\n")[0]
        list = re.findall('.*\w+',name)
        if 1 > len(list):
            list = re.findall(ur'[\u4e00-\u9fa5]',name.decode('utf-8'))
            if 1 > len(list):
                errorinfo("Error No.23: Some error on ext-change document:.*\w+")
    else:
        errorinfo("Error No.24: Some error on ext-change document:.*\w+")
    if "ext-change others:" in msg:
        print "hello"
    else:
        errorinfo("Error No.25: Some error on ext-change others:.*\w+")
    return 0

if __name__ == "__main__":
    file_object = open('msg.log', 'r')
    msg = file_object.read()
    file_object.close( )
    msg_check(msg)
    sys.exit()
    
